import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:kq_flutter_core_widget/utils/kq_screen_util.dart';
import 'package:get/get.dart';
import 'package:kq_flutter_core_widget/utils/ex/list_ex.dart';
import 'package:kq_flutter_widgets/widgets/steps/kq_step_number_bar.dart';
import '../../resources/kq_theme_colors.dart';
import '../../resources/l10n/kq_intl.dart';
import 'package:kq_flutter_core_widget/utils/text_filed_utils.dart';
import 'package:kq_flutter_core_widget/widgets/button/kq_ink_well.dart';
import 'package:kq_flutter_widgets/theme/kq_theme_manager.dart';

import '../divider/kq_divider.dart';
import '../textFiled/kq_textfiled.dart';

/// 数字步进对话框
///
/// 数字减 + 输入框 + 数字加
///
/// @author 周卓
///
class KqStepNumberDialog extends AlertDialog {
  /// 标题
  final String? titleString;

  /// 步进值，默认1
  final num step;

  /// 最小值，默认0
  final num min;

  /// 最大值
  final num? max;

  /// 是否支持小数，默认false
  final bool decimal;

  /// 是否可编辑
  final bool editable;

  /// 输入框内容控制器
  final TextEditingController editController;

  /// 左边按钮文字，默认"取消"
  final String? leftBtnText;

  /// 左边按钮文字，默认"确定"
  final String? rightBtnText;

  /// 左边按钮回调
  final Function(String value)? onLeftBtnTap;

  /// 右边按钮回调
  final Function(String value)? onRightBtnTap;

  const KqStepNumberDialog({
    Key? key,
    this.titleString,
    this.decimal = false,
    this.editable = true,
    this.min = 0,
    this.step = 1,
    this.max,
    required this.editController,
    this.leftBtnText,
    this.rightBtnText,
    this.onLeftBtnTap,
    this.onRightBtnTap,
  }) : super(key: key);

  @override
  Widget build(BuildContext context) {
    bool isLangScape = context.orientation == Orientation.landscape;

    final List<Widget> children = <Widget>[];

    /// 上边距
    children.add(SizedBox(
      width: 1.r,
      height: 24.r,
    ));

    if (titleString != null) {
      children.add(Container(
        padding: EdgeInsets.symmetric(horizontal: 16.r),
        alignment: Alignment.center,
        child: Text(
          titleString!,
          style: TextStyle(
              fontSize: 16.sp,
              color:
                  KqThemeManager.instance.getConfig().dialogConfig.titleColor ??
                      KqThemeColors.text26,
              fontWeight: FontWeight.w600),
        ),
      ));
      children.add(SizedBox(
        width: 1.r,
        height: 24.r,
      ));
    }

    /// 输入框
    children.add(Container(
      margin: EdgeInsets.symmetric(horizontal: 16.r),
      alignment: Alignment.center,
      child: KqStepNumberBar(
        decimal: decimal,
        editable: editable,
        min: min,
        max: max,
        step: step,
        editController: editController,
      ),
    ));

    /// 下边距
    children.add(SizedBox(
      width: 1.r,
      height: 24.r,
    ));

    /// 按钮
    children.add(const KqDivider());

    children.add(Row(
      children: [
        Expanded(
            child: KqInkWell(
          onTap: () {
            if (onLeftBtnTap != null) {
              var text = editController.text;
              if (text.isEmpty) {
                text = min.toString();
              }
              onLeftBtnTap!(text);
            }
          },
          child: Container(
            alignment: Alignment.center,
            width: double.infinity,
            height: 48.r,
            child: Text(
              leftBtnText ?? KqIntl.currentResource.cancel,
              style: TextStyle(
                  fontSize: 16.sp,
                  color: KqThemeManager.instance
                          .getConfig()
                          .dialogConfig
                          .cancelBtnColor ??
                      KqThemeColors.text8C),
            ),
          ),
        )),
        KqVerticalDivider(
          height: 48.r,
        ),
        Expanded(
            child: KqInkWell(
          onTap: () {
            if (onRightBtnTap != null) {
              var text = editController.text;
              if (text.isEmpty) {
                text = min.toString();
              }
              onRightBtnTap!(text);
            }
          },
          child: Container(
            alignment: Alignment.center,
            width: double.infinity,
            height: 48.r,
            child: Text(
              rightBtnText ?? KqIntl.currentResource.confirm,
              style: TextStyle(
                  fontSize: 16.sp,
                  color: KqThemeManager.getDialogConfig().mainBtnColor ??
                      KqThemeColors.textLightBlue),
            ),
          ),
        ))
      ],
    ));

    Widget dialogChild = Column(
      mainAxisSize: MainAxisSize.min,
      crossAxisAlignment: CrossAxisAlignment.stretch,
      children: children,
    );
    var padding = MediaQuery.of(context).viewInsets.bottom;
    return UnconstrainedBox(
        child: Padding(
      padding: EdgeInsets.only(bottom: padding),
      child: SizedBox(
          width: 320.r,
          child: Material(
              shape: RoundedRectangleBorder(
                  borderRadius: BorderRadius.all(Radius.circular(
                      KqThemeManager.instance.getConfig().dialogConfig.radius ??
                          8.r))),
              color: KqThemeColors.bgWhite,
              child: LimitedBox(
                maxHeight: isLangScape ? 350.r : 400.r,
                child: ClipRRect(
                  borderRadius: BorderRadius.all(Radius.circular(
                      KqThemeManager.instance.getConfig().dialogConfig.radius ??
                          8.r)),
                  child: dialogChild,
                ),
              ))),
    ));
  }
}
